import axios from 'axios'
import { Message } from 'element-ui'
import { getToken } from '@/utils/token'
import store from '@/store'
import baseUrl from './baseUrl'



axios.defaults.baseURL = `${baseUrl}/engtest`

//请求拦截器
//发送请求前做的什么
axios.interceptors.request.use(config => {
    config.headers['Authorization'] = getToken();
    return config;
});


axios.interceptors.response.use(
    response => {
        if (response.status == 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    error => {
        const { data: { msg } } = error.response;
        if (msg == '该用户未注册！') {
            store.dispatch('Logout');
        }
        Message({
            message: msg || '服务器不给力！',
            type: 'warning',
            duration: 1000
        });
        return Promise.reject(error.response);
    }
);

export default axios;